﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SoftwareDiscipline.SharePoint.UserManagement.Data
{
    public class AllRolesDataSource
    {
     
        /// <summary>
        /// Add New Role Name 
        /// </summary>
        /// <param name="newRole">Name of the new Role</param>
        /// <returns></returns>
        public string AddNewRole(string newRole)
        {
            string status = string.Empty;
            if (FBARoleProvider.RoleExists(newRole))
            {
                status = "Exist";
                return status;
            }

            try
            {
                Roles.CreateRole(newRole);
                status = "success";

            }
            catch (Exception e)
            {
                status = "Unable to Add New Role \n\n" + e.Message;

            }
            return status;
        }

        /// <summary>
        /// This method edit the role from the FB DB
        /// </summary>
        /// <param name="originalName"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public string EditRole(string originalName, string newName)
        {
            
                string[] STATUSMESSAGES = new string[5];
            STATUSMESSAGES[0] = "New role name is same as current role name, please enter different role name"; // this.GetGlobalResourceObject("swusrmgmt", "AllRolesDataSource_SameRoleName") as string;
            STATUSMESSAGES[1] = "Role name already exist"; //this.GetGlobalResourceObject("swusrmgmt", "AllRolesDataSource_RoleNameExist") as string;
            STATUSMESSAGES[2] = "Role name cannot be edited"; // this.GetGlobalResourceObject("swusrmgmt", "AllRolesDataSource_EditRole_Error") as string;
                STATUSMESSAGES[3] = "SUCCESS";
                STATUSMESSAGES[4] = "FAIL";

                string status = string.Empty;

                try
                {
                    if (originalName.ToLower() == newName.ToLower())
                    {
                        status = STATUSMESSAGES[0];
                        return status;
                    }
                    if (FBARoleProvider.RoleExists(newName))
                    {
                        status = STATUSMESSAGES[1];
                        return status;
                    }

                    // Get All users for the current role
                    string[] allusers = FBARoleProvider.GetUsersInRole(originalName);
                    string[] role = { newName };
                    //Create new Role 
                    FBARoleProvider.CreateRole(newName);
                    //Assign users to the new Role
                    if (allusers.Count() != 0)
                    {
                        FBARoleProvider.AddUsersToRoles(allusers, role);
                    }
                    //Delete old Role
                    FBARoleProvider.DeleteRole(originalName, false);
                    status = STATUSMESSAGES[3];

                }
                catch (Exception)
                {
                    status = STATUSMESSAGES[4];

                }

            return status;
        }

        /// <summary>
        /// Delte the existing Role 
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public string DelteRole(string roleName)
        {
            string status = string.Empty;

            string[] STATUSMESSAGES = new string[3];
            STATUSMESSAGES[0] = "Role name does not exist"; // this.GetGlobalResourceObject("swusrmgmt", "AllRolesDataSource_DeleteRole") as string;
            STATUSMESSAGES[1] = "SUCCESS";
            STATUSMESSAGES[2] = "FAIL";

            try
            {

            
            if (!FBARoleProvider.RoleExists(roleName))
            {
                status = STATUSMESSAGES[0];
                return status;
            }
            //Deleting Role Name 
            FBARoleProvider.DeleteRole(roleName, false);
            status = STATUSMESSAGES[1];
                return status;
            }
            catch (Exception)
            {

                status = STATUSMESSAGES[2];
                return status;
            }

        }

        /// <summary>
        /// Returns All users by Role Name 
        /// </summary>
        /// <param name="roleName"></param>
        /// <returns></returns>
        public string[] AllUsersByRole(string roleName)
        {
            try
            {
                string[] allUsers = FBARoleProvider.GetUsersInRole(roleName);
                return allUsers;
            }
            catch(Exception e)
            {
                
            }
            return null;

        }

        private static RoleProvider FBARoleProvider
        {
           
            get
            {
                SPWebApplication webApplication = SPContext.Current.Site.WebApplication;
                if (webApplication != null)
                {
                    //find the current zone 
                    SPUrlZone currentZone = SPContext.Current.Site.Zone;

                    SqlRoleProvider role = webApplication.GetSqlRoleProvider(currentZone);

                    return role;
                }
                return null;

            }
        }

        /// <summary>
        /// Returns number of Roles available in the database
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return FBARoleProvider.GetAllRoles().Count();
        }

        public DataTable GetAllRoles(int startRow, int pageSize)
        {
            DataTable roleTable = new DataTable();
            DataColumn dt = new DataColumn("RoleName", typeof (string));
            roleTable.Columns.Add(dt);

            try
            {
            string[] _roles =FBARoleProvider.GetAllRoles();

                foreach (string role in _roles)
                {
                    DataRow row = roleTable.NewRow();
                    row["RoleName"] = role;
                    roleTable.Rows.Add(row);
                }

                return roleTable;

            }
            catch(Exception e)
            {

            }

            return null;
        }
    }
}


